package com.neutech.maplestory.client;

import com.neutech.maplestory.entity.Arrow;
import com.neutech.maplestory.entity.Direction;
import com.neutech.maplestory.entity.Hero;
import com.neutech.maplestory.entity.Mob;
import com.neutech.maplestory.util.MapleStoryFrame;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 游戏的主入口文件
 */
public class MapleStoryClient extends MapleStoryFrame {
    public Hero hero = new Hero(this,300,500);
    public List<Arrow> arrows = new CopyOnWriteArrayList<>();
//    public Arrow arrow = new Arrow(this,400,500, Direction.LEFT);

    public List<Mob> mobs = new CopyOnWriteArrayList<>();

    {
        for (int i = 0; i < 10; i++) {
            Mob mob = new Mob(this, 700 + (i * 80), 530, 5);
            mobs.add(mob);
        }
    }

    @Override
    public void init(){
        super.init();
        //添加监听器
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                hero.keyPressed(e);
            }

            @Override
            public void keyReleased(KeyEvent e){
                hero.keyReleased(e);
            }
        });
    }

    @Override
    public void paint(Graphics g){
        hero.draw(g);
//        arrow.draw(g);

        //使用迭代器和 CopyOnWriteArrayList 解决数组索引移动 (非线程安全问题)
        Iterator<Arrow> itArrows = arrows.iterator();
        while (itArrows.hasNext()){
            Arrow arrow = itArrows.next();
            arrow.draw(g);
            arrow.hit(mobs);
        }

        Iterator<Mob> itMobs = mobs.iterator();
        while (itMobs.hasNext()){
            Mob mob = itMobs.next();
            mob.draw(g);
        }

        Font f = g.getFont();
        g.setFont(new Font("微软雅黑",Font.BOLD,30));
        g.drawString("人物的方向为：" + hero.dir,100,100);
        g.drawString("人物的动作为：" + hero.action,100,150);
        g.drawString("count：" + hero.count,100,200);
        g.drawString("arrows.size：" + arrows.size(),100,250);
        g.setFont(f);
    }

    public static void main(String[] args) {
        new MapleStoryClient().init();
    }
}
